'''
    自动取款机
    1：账户信息数据源
    2：登陆账户
    3：查看账户信息
    4：存钱
    5：取钱
    6：退出系统
    7：界面和交互
'''

account_list = [
    {'name': '小明', 'psd': 123, 'money': 1555},
    {'name': '小张', 'psd': 321, 'money': 1845},
    {'name': '小红', 'psd': 456, 'money': 1955}
]
# 创建一个空列表，存储登陆后的账户信息
p = []


# 登陆账户
def login(name, psd):
    '''

    :return:
    '''
    flag = 0
    for i in account_list:
        if name == i['name']:
            if psd == i['psd']:
                print('登陆成功')
                p.append(i)  # 登陆成功后将值传入列表保存
                return
            else:
                print('密码错误')
                return
        else:
            print('账户错误,请重试')
            return


# 查看账户信息


def show_account():
    '''

    :return:
    '''
    if len(account_list) == 0:
        print('=' * 20, '账户信息为空', '=' * 20)
        return
    print('|{0:<5}|{1:<10}|{2:<6}|{3:<10}|'.format('sid', 'name', 'psd', 'money'))
    print('-' * 30)
    for i, stu_dict in enumerate(account_list):
        print('|{0:<5}|{1:<8}|{2:<6}|{3:<10}|'.format(i + 1, stu_dict['name'], stu_dict['psd'], stu_dict['money']))


# 添加账户信息
def add_account(name, psd, money):
    dict = {}
    dict['name'] = name
    dict['psd'] = psd
    dict['money'] = money
    account_list.append(dict)


# 取钱
def draw_money(sumMoney):
    for i in account_list:
        if p[0]['name'] == i['name']:  # 验证登陆账户是否在源账户信息中
            if p[0]['money'] >= sumMoney:
                sum = i['money']
                sum -= sumMoney
                print('取走{}元,剩下{}元'.format(sumMoney, sum))
            else:
                print('余额不足')
                return


# 存钱
def add_money(sumMoney):
    for i in account_list:
        if p[0]['name'] == i['name']:  # 验证登陆账户是否在源账户信息中
            sum = i['money']
            sum += sumMoney
            print('存入{}元,剩下{}元'.format(sumMoney, sum))


# 退出系统
def logout():
    pass


# 界面交互
while True:
    # 输出应该初始界面
    print('=' * 10, '自动存取款机', '=' * 10)
    if len(p) == 0:
        name = input('请输入你的账户:')
        pwd = int(input('请输入你的密码:'))
        login(name, pwd)

    else:
        print('{:1} {:12} {:10}'.format(' ', '1:登陆账户', '2:查看账户信息'))
        print('{:1} {:13} {:10}'.format(' ', '3:存钱', '4:取钱'))
        print('{:1} {:12}'.format(' ', '5:退出系统'))
        print('=' * 10, '用户操作界面', '=' * 10)
        key = input('请输入对应的选择:')
        # 根据输入值 执行对应程序
        if key == '1':
            input('按回车继续')
        elif key == '2':
            show_account()
        elif key == '3':
            sumMoney = int(input('请输入你要存的钱:'))
            add_money(sumMoney)
        elif key == '4':
            sumMoney = int(input('请输入你要取的钱:'))
            draw_money(sumMoney)
        elif key == '5':
            print('拜拜')
            break
